using Dapr.Client;
using Furion.DynamicApiController;
using Kevin.Dapr.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Kevin.Dapr.Api.Controllers
{
    /// <summary>
    /// 订单
    /// </summary>
    public partial class DaprHttpController : IDynamicApiController
    {
        /// <summary>
        /// dapr get无参
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<string> ThrowException()
        {
            var data = await _daprClient.InvokeMethodAsync<DaprStructValue<string>>(HttpMethod.Get, "daprhttpservice", "/api/daprhttp/throwexception");
            return data.Value;
        }

        /// <summary>
        /// dapr get无参
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<string> GetByNoParameters()
        {
            var data = await _daprClient.InvokeMethodAsync<DaprStructValue<string>>(HttpMethod.Get, "daprhttpservice", "/api/daprhttp/getbynoparameters");
            return data.Value;
        }

        /// <summary>
        /// dapr get有参
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet, QueryParameters]
        public async Task<List<string>> GetByParameters(int page, int size)
        {
            var data = await _daprClient.InvokeMethodAsync<List<string>>(HttpMethod.Get, "daprhttpservice", $"/api/daprhttp/getbyparameters/{page}/{size}");
            return data;
        }

        /// <summary>
        /// post 带参数
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AddOrderInput> PostByParameters(AddOrderInput input)
        {
            var data = await _daprClient.InvokeMethodAsync<AddOrderInput, AddOrderInput>(HttpMethod.Post, "daprhttpservice", "/api/daprhttp/postbyparameters", input);
            return data;
        }

        /// <summary>
        /// 发布订阅
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task Subscription(string content)
        {
            var data = new SendMessageInput("18108090568", content);
            await _daprClient.PublishEventAsync<SendMessageInput>("pubsub", "user_topic", data);
            Console.WriteLine($"短信事件发送成功:{JsonConvert.SerializeObject(data)}");
        }
    }

    public partial class DaprHttpController
    {
        private readonly DaprClient _daprClient;

        public DaprHttpController(DaprClient daprClient)
        {
            _daprClient = daprClient;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="Address"></param>
    /// <param name="number"></param>
    public record AddOrderInput(long Id, string Address, int number);

    public record SendMessageInput(string cellphone, string content);
}
